package sys

import (
	log "fisherman/common/logger"
	"fmt"
	"os"
	"os/exec"
	"runtime"
	"strings"
)

// 读取系统CPU逻辑核心数量（超频的CPU一般是物理核心数的两倍）
func GetLogicalCPUCount() int {
	if runtime.GOOS == "windows" {
		cmd := exec.Command("powershell", "Get-WmiObject", "-Class", "Win32_ComputerSystem", "|", "Select-Object", "-ExpandProperty", "NumberOfLogicalProcessors")
		output, err := cmd.Output()
		if err != nil {
			log.Errorf("无法获取逻辑线程数: %v", err)
			return 0
		}
		logicalCPUs := strings.TrimSpace(string(output))
		var result int
		_, _ = fmt.Sscanf(logicalCPUs, "%d", &result)
		return result
	} else {
		data, err := os.ReadFile("/proc/cpuinfo")
		if err != nil {
			log.Errorf("无法读取 /proc/cpuinfo：%v", err)
			return 0
		}
		logicalCPUs := strings.Count(string(data), "processor")
		return logicalCPUs
	}
}
